﻿using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using KeanuMvc.Reference;
using HISModels.Entities;
using System.Net.Http;
using Newtonsoft.Json;
using System.Data.Objects;
using HISModels.ViewModel;

namespace HISRestServices.Services {

    [ServiceContract()]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class Workflow : BaseServices, IDisposable {

        #region Field
        private HttpStatusCode statusCode = new HttpStatusCode();
        private string detailMessage = string.Empty;
        #endregion

       
        [WebGet(UriTemplate = "template/master/")]
        public HttpResponseMessage<List<WorkflowMasterTemplate>> GetListWFMaster() {
            HttpResponseMessage<List<WorkflowMasterTemplate>> returnValue = null;
            List<WorkflowMasterTemplate> listWFMaster = null;
            try {
                listWFMaster = AppContext.WorkflowMasterTemplates.ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<WorkflowMasterTemplate>>(statusCode);
                returnValue.Content = new ObjectContent<List<WorkflowMasterTemplate>>(listWFMaster);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebGet(UriTemplate = "template/master/{id}")]
        public HttpResponseMessage<List<WorkflowMasterTemplate>> GetListWFMasterByUsrId(string id) {
            HttpResponseMessage<List<WorkflowMasterTemplate>> returnValue = null;
            List<WorkflowMasterTemplate> listWFMaster = null;
            try {
                listWFMaster = (from b in AppContext.WorkflowMasterTemplates
                               join c in AppContext.SysMemberships on b.OwnerUserId equals c.UserId
                               where b.OwnerUserId == id
                               select b).ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<WorkflowMasterTemplate>>(statusCode);
                returnValue.Content = new ObjectContent<List<WorkflowMasterTemplate>>(listWFMaster);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebGet(UriTemplate = "template/{id}")]
        public HttpResponseMessage<List<WorkflowTemplate>> GetListWFTemplate(int id) {
            HttpResponseMessage<List<WorkflowTemplate>> returnValue = null;
            List<WorkflowTemplate> listWFTemplate = null;
            try {
                listWFTemplate = (from b in AppContext.WorkflowTemplates
                                 join c in AppContext.WorkflowMasterTemplates on b.WorkflowTemplateMasterId equals c.Id
                                 where b.WorkflowTemplateMasterId == id 
                                 select b).ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<WorkflowTemplate>>(statusCode);
                returnValue.Content = new ObjectContent<List<WorkflowTemplate>>(listWFTemplate);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebGet(UriTemplate = "employee/superior/{id}")]
        public HttpResponseMessage<List<Employee>> GetListManajer(int id) {
            HttpResponseMessage<List<Employee>> returnValue = null;
            List<Employee> listmanajer = null;
            string SuperiorNumber = GetSuperiorNumber(1);
            Employee employee = null;
            try {
                listmanajer = new List<Employee> ();
                var a = from b in AppContext.Employees
                         select b;

                a=a.Where(d => d.EmployeeNumber == SuperiorNumber);
                employee = a.First();
                listmanajer.Add(employee);
                SuperiorNumber = employee.SuperiorNumber;
                
                while (SuperiorNumber!=null) {

                    a = a.Where(d => d.EmployeeNumber == SuperiorNumber);
                    employee = a.First();
                    SuperiorNumber = employee.SuperiorNumber;
                    listmanajer.Add(employee);
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<Employee>>(statusCode);
                returnValue.Content = new ObjectContent<List<Employee>>(listmanajer);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        public string GetSuperiorNumber(int id) {
            string returnValue = string.Empty;
            try {
                returnValue= (from c in AppContext.Employees 
                             where c.Id == id 
                             select c).First().SuperiorNumber.ToString();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            return returnValue;
        }

        void IDisposable.Dispose() {
            try {
                base.Dispose();
                GC.SuppressFinalize(this);
            } catch { }
        }
    }
}